<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="支付流水ID，取值CATEGORY的ID" prop="payOrderId">
        <el-input v-model="dataForm.payOrderId" placeholder="支付流水ID，取值CATEGORY的ID"></el-input>
      </el-form-item>
      <el-form-item label="交易子表流水ID，取值TRADE_SUB_ORDER的ID" prop="tradeSubGoodOrderId">
        <el-input v-model="dataForm.tradeSubGoodOrderId" placeholder="交易子表流水ID，取值TRADE_SUB_ORDER的ID"></el-input>
      </el-form-item>
      <el-form-item label="交易关联商品快照流水ID，取值TRADE_GOODS的ID" prop="tradeGoodsId">
        <el-input v-model="dataForm.tradeGoodsId" placeholder="交易关联商品快照流水ID，取值TRADE_GOODS的ID"></el-input>
      </el-form-item>
      <el-form-item label="交易关联用户流水ID，取值USER的ID" prop="userId">
        <el-input v-model="dataForm.userId" placeholder="交易关联用户流水ID，取值USER的ID"></el-input>
      </el-form-item>
      <el-form-item label="用户选择的支付方式的ID,取值表PAY_TOOL表" prop="payToolId">
        <el-input v-model="dataForm.payToolId" placeholder="用户选择的支付方式的ID,取值表PAY_TOOL表"></el-input>
      </el-form-item>
      <el-form-item label="收货人的姓名,用户页面填写,默认取值表USER_ADDRESS" prop="consignee">
        <el-input v-model="dataForm.consignee" placeholder="收货人的姓名,用户页面填写,默认取值表USER_ADDRESS"></el-input>
      </el-form-item>
      <el-form-item label="收货人的固定电话,用户页面填写,默认取值表USER_ADDRESS" prop="tel">
        <el-input v-model="dataForm.tel" placeholder="收货人的固定电话,用户页面填写,默认取值表USER_ADDRESS"></el-input>
      </el-form-item>
      <el-form-item label="收货人的手机,用户页面填写,默认取值表USER_ADDRESS" prop="mobile">
        <el-input v-model="dataForm.mobile" placeholder="收货人的手机,用户页面填写,默认取值表USER_ADDRESS"></el-input>
      </el-form-item>
      <el-form-item label="收货人的最佳送货时间,用户页面填写,默认取值表USER_ADDRESS" prop="bestTime">
        <el-input v-model="dataForm.bestTime" placeholder="收货人的最佳送货时间,用户页面填写,默认取值表USER_ADDRESS"></el-input>
      </el-form-item>
      <el-form-item label="收货人的详细地址,用户页面填写,默认取值表USER_ADDRESS" prop="address">
        <el-input v-model="dataForm.address" placeholder="收货人的详细地址,用户页面填写,默认取值表USER_ADDRESS"></el-input>
      </el-form-item>
      <el-form-item label="用户选择的配送方式ID,取值表SHIPPING" prop="shippingId">
        <el-input v-model="dataForm.shippingId" placeholder="用户选择的配送方式ID,取值表SHIPPING"></el-input>
      </el-form-item>
      <el-form-item label="订单配送时间" prop="shippingTime">
        <el-input v-model="dataForm.shippingTime" placeholder="订单配送时间"></el-input>
      </el-form-item>
      <el-form-item label="订单附言,由用户提交订单前填写" prop="postscript">
        <el-input v-model="dataForm.postscript" placeholder="订单附言,由用户提交订单前填写"></el-input>
      </el-form-item>
      <el-form-item label="订单应付款金额即商品总额加配送费加包装费等" prop="orderAmount">
        <el-input v-model="dataForm.orderAmount" placeholder="订单应付款金额即商品总额加配送费加包装费等"></el-input>
      </el-form-item>
      <el-form-item label="商品的总金额" prop="goodsAmount">
        <el-input v-model="dataForm.goodsAmount" placeholder="商品的总金额"></el-input>
      </el-form-item>
      <el-form-item label="配送费用即外卖系统比较重要" prop="shippingFee">
        <el-input v-model="dataForm.shippingFee" placeholder="配送费用即外卖系统比较重要"></el-input>
      </el-form-item>
      <el-form-item label="包装费用即点餐打包费外卖系统比较重要" prop="packFee">
        <el-input v-model="dataForm.packFee" placeholder="包装费用即点餐打包费外卖系统比较重要"></el-input>
      </el-form-item>
      <el-form-item label="保价费用" prop="insureFee">
        <el-input v-model="dataForm.insureFee" placeholder="保价费用"></el-input>
      </el-form-item>
      <el-form-item label="支付手续费用,跟支付方式的配置相关,取值表PAYMENT" prop="payFee">
        <el-input v-model="dataForm.payFee" placeholder="支付手续费用,跟支付方式的配置相关,取值表PAYMENT"></el-input>
      </el-form-item>
      <el-form-item label="折扣总金额即积分和红包抵扣总额" prop="discount">
        <el-input v-model="dataForm.discount" placeholder="折扣总金额即积分和红包抵扣总额"></el-input>
      </el-form-item>
      <el-form-item label="消费积分数量，商品可用积分,用户拥有积分中最小者" prop="integral">
        <el-input v-model="dataForm.integral" placeholder="消费积分数量，商品可用积分,用户拥有积分中最小者"></el-input>
      </el-form-item>
      <el-form-item label="使用消费积分金额抵扣的金额" prop="integralMoney">
        <el-input v-model="dataForm.integralMoney" placeholder="使用消费积分金额抵扣的金额"></el-input>
      </el-form-item>
      <el-form-item label="优惠类型，即红包ID, USER_BONUS的BONUS_ID" prop="bonusId">
        <el-input v-model="dataForm.bonusId" placeholder="优惠类型，即红包ID, USER_BONUS的BONUS_ID"></el-input>
      </el-form-item>
      <el-form-item label="优惠额度，使用红包抵扣的金额" prop="bonusMoney">
        <el-input v-model="dataForm.bonusMoney" placeholder="优惠额度，使用红包抵扣的金额"></el-input>
      </el-form-item>
      <el-form-item label="使用用户余额抵扣的金额，取用户设定余额,用户可用余额,订单金额中最小者" prop="balanceMoney">
        <el-input v-model="dataForm.balanceMoney" placeholder="使用用户余额抵扣的金额，取用户设定余额,用户可用余额,订单金额中最小者"></el-input>
      </el-form-item>
      <el-form-item label="需付款金额即减去优惠总金额后剩余的金额" prop="moneyPaid">
        <el-input v-model="dataForm.moneyPaid" placeholder="需付款金额即减去优惠总金额后剩余的金额"></el-input>
      </el-form-item>
      <el-form-item label="订单状态(数字0：未插入，1已确认; 2已取消; 3无效; 4退货)" prop="orderStatus">
        <el-input v-model="dataForm.orderStatus" placeholder="订单状态(数字0：未插入，1已确认; 2已取消; 3无效; 4退货)"></el-input>
      </el-form-item>
      <el-form-item label="订单总评分" prop="orderScore">
        <el-input v-model="dataForm.orderScore" placeholder="订单总评分"></el-input>
      </el-form-item>
      <el-form-item label="操作员" prop="operator">
        <el-input v-model="dataForm.operator" placeholder="操作员"></el-input>
      </el-form-item>
      <!--<el-form-item label="创建即上传时间" prop="createUtc">
        <el-input v-model="dataForm.createUtc" placeholder="创建即上传时间"></el-input>
      </el-form-item>
      <el-form-item label="修改即更新时间" prop="modifyUtc">
        <el-input v-model="dataForm.modifyUtc" placeholder="修改即更新时间"></el-input>
      </el-form-item>
      <el-form-item label="是否删除" prop="isDeleted">
        <el-input v-model="dataForm.isDeleted" placeholder="是否删除"></el-input>
      </el-form-item>-->

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import isInteger from 'lodash/isInteger'
  import moment from 'moment'
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          payOrderId: '',
          tradeSubGoodOrderId: '',
          tradeGoodsId: '',
          userId: '',
          payToolId: '',
          consignee: '',
          tel: '',
          mobile: '',
          bestTime: '',
          address: '',
          shippingId: '',
          shippingTime: '',
          postscript: '',
          orderAmount: '',
          goodsAmount: '',
          shippingFee: '',
          packFee: '',
          insureFee: '',
          payFee: '',
          discount: '',
          integral: '',
          integralMoney: '',
          bonusId: '',
          bonusMoney: '',
          balanceMoney: '',
          moneyPaid: '',
          orderStatus: '',
          orderScore: '',
          operator: '',
          createUtc: moment(new Date()).format('YYYY-MM-DD HH:mm:ss'),
          modifyUtc: moment(new Date()).format('YYYY-MM-DD HH:mm:ss'),
          isDeleted: '',
          version: ''
        },
        dataRule: {
          payOrderId: [
            { required: true, message: '支付流水ID，取值CATEGORY的ID不能为空', trigger: 'blur' }
          ],
          tradeSubGoodOrderId: [
            { required: true, message: '交易子表流水ID，取值TRADE_SUB_ORDER的ID不能为空', trigger: 'blur' }
          ],
          tradeGoodsId: [
            { required: true, message: '交易关联商品快照流水ID，取值TRADE_GOODS的ID不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '交易关联用户流水ID，取值USER的ID不能为空', trigger: 'blur' }
          ],
          payToolId: [
            { required: true, message: '用户选择的支付方式的ID,取值表PAY_TOOL表不能为空', trigger: 'blur' }
          ],
          consignee: [
            { required: true, message: '收货人的姓名,用户页面填写,默认取值表USER_ADDRESS不能为空', trigger: 'blur' }
          ],
          tel: [
            { required: true, message: '收货人的固定电话,用户页面填写,默认取值表USER_ADDRESS不能为空', trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: '收货人的手机,用户页面填写,默认取值表USER_ADDRESS不能为空', trigger: 'blur' }
          ],
          bestTime: [
            { required: true, message: '收货人的最佳送货时间,用户页面填写,默认取值表USER_ADDRESS不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '收货人的详细地址,用户页面填写,默认取值表USER_ADDRESS不能为空', trigger: 'blur' }
          ],
          shippingId: [
            { required: true, message: '用户选择的配送方式ID,取值表SHIPPING不能为空', trigger: 'blur' }
          ],
          shippingTime: [
            { required: true, message: '订单配送时间不能为空', trigger: 'blur' }
          ],
          postscript: [
            { required: true, message: '订单附言,由用户提交订单前填写不能为空', trigger: 'blur' }
          ],
          orderAmount: [
            { required: true, message: '订单应付款金额即商品总额加配送费加包装费等不能为空', trigger: 'blur' }
          ],
          goodsAmount: [
            { required: true, message: '商品的总金额不能为空', trigger: 'blur' }
          ],
          shippingFee: [
            { required: true, message: '配送费用即外卖系统比较重要不能为空', trigger: 'blur' }
          ],
          packFee: [
            { required: true, message: '包装费用即点餐打包费外卖系统比较重要不能为空', trigger: 'blur' }
          ],
          insureFee: [
            { required: true, message: '保价费用不能为空', trigger: 'blur' }
          ],
          payFee: [
            { required: true, message: '支付手续费用,跟支付方式的配置相关,取值表PAYMENT不能为空', trigger: 'blur' }
          ],
          discount: [
            { required: true, message: '折扣总金额即积分和红包抵扣总额不能为空', trigger: 'blur' }
          ],
          integral: [
            { required: true, message: '消费积分数量，商品可用积分,用户拥有积分中最小者不能为空', trigger: 'blur' }
          ],
          integralMoney: [
            { required: true, message: '使用消费积分金额抵扣的金额不能为空', trigger: 'blur' }
          ],
          bonusId: [
            { required: true, message: '优惠类型，即红包ID, USER_BONUS的BONUS_ID不能为空', trigger: 'blur' }
          ],
          bonusMoney: [
            { required: true, message: '优惠额度，使用红包抵扣的金额不能为空', trigger: 'blur' }
          ],
          balanceMoney: [
            { required: true, message: '使用用户余额抵扣的金额，取用户设定余额,用户可用余额,订单金额中最小者不能为空', trigger: 'blur' }
          ],
          moneyPaid: [
            { required: true, message: '需付款金额即减去优惠总金额后剩余的金额不能为空', trigger: 'blur' }
          ],
          orderStatus: [
            { required: true, message: '订单状态(数字0：未插入，1已确认; 2已取消; 3无效; 4退货)不能为空', trigger: 'blur' }
          ],
          orderScore: [
            { required: true, message: '订单总评分不能为空', trigger: 'blur' }
          ],
          operator: [
            { required: true, message: '操作员不能为空', trigger: 'blur' }
          ],
          createUtc: [
            { required: true, message: '创建即上传时间不能为空', trigger: 'blur' }
          ],
          modifyUtc: [
            { required: true, message: '修改即更新时间不能为空', trigger: 'blur' }
          ],
          isDeleted: [
            { required: true, message: '是否删除不能为空', trigger: 'blur' }
          ],
          version: [
            { required: true, message: '版本号不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl('/generator/upktradeorder/info' + (isInteger(this.dataForm.id) ? `/${this.dataForm.id}` : '')),
              method: 'get'
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.payOrderId = data.upkTradeorder.payOrderId
                this.dataForm.tradeSubGoodOrderId = data.upkTradeorder.tradeSubGoodOrderId
                this.dataForm.tradeGoodsId = data.upkTradeorder.tradeGoodsId
                this.dataForm.userId = data.upkTradeorder.userId
                this.dataForm.payToolId = data.upkTradeorder.payToolId
                this.dataForm.consignee = data.upkTradeorder.consignee
                this.dataForm.tel = data.upkTradeorder.tel
                this.dataForm.mobile = data.upkTradeorder.mobile
                this.dataForm.bestTime = data.upkTradeorder.bestTime
                this.dataForm.address = data.upkTradeorder.address
                this.dataForm.shippingId = data.upkTradeorder.shippingId
                this.dataForm.shippingTime = data.upkTradeorder.shippingTime
                this.dataForm.postscript = data.upkTradeorder.postscript
                this.dataForm.orderAmount = data.upkTradeorder.orderAmount
                this.dataForm.goodsAmount = data.upkTradeorder.goodsAmount
                this.dataForm.shippingFee = data.upkTradeorder.shippingFee
                this.dataForm.packFee = data.upkTradeorder.packFee
                this.dataForm.insureFee = data.upkTradeorder.insureFee
                this.dataForm.payFee = data.upkTradeorder.payFee
                this.dataForm.discount = data.upkTradeorder.discount
                this.dataForm.integral = data.upkTradeorder.integral
                this.dataForm.integralMoney = data.upkTradeorder.integralMoney
                this.dataForm.bonusId = data.upkTradeorder.bonusId
                this.dataForm.bonusMoney = data.upkTradeorder.bonusMoney
                this.dataForm.balanceMoney = data.upkTradeorder.balanceMoney
                this.dataForm.moneyPaid = data.upkTradeorder.moneyPaid
                this.dataForm.orderStatus = data.upkTradeorder.orderStatus
                this.dataForm.orderScore = data.upkTradeorder.orderScore
                this.dataForm.operator = data.upkTradeorder.operator
                this.dataForm.createUtc = data.upkTradeorder.createUtc
                this.dataForm.modifyUtc = data.upkTradeorder.modifyUtc
                this.dataForm.isDeleted = data.upkTradeorder.isDeleted
                this.dataForm.version = data.upkTradeorder.version
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            var params = {
              'id': this.dataForm.id || undefined,
              'payOrderId': this.dataForm.payOrderId,
              'tradeSubGoodOrderId': this.dataForm.tradeSubGoodOrderId,
              'tradeGoodsId': this.dataForm.tradeGoodsId,
              'userId': this.dataForm.userId,
              'payToolId': this.dataForm.payToolId,
              'consignee': this.dataForm.consignee,
              'tel': this.dataForm.tel,
              'mobile': this.dataForm.mobile,
              'bestTime': this.dataForm.bestTime,
              'address': this.dataForm.address,
              'shippingId': this.dataForm.shippingId,
              'shippingTime': this.dataForm.shippingTime,
              'postscript': this.dataForm.postscript,
              'orderAmount': this.dataForm.orderAmount,
              'goodsAmount': this.dataForm.goodsAmount,
              'shippingFee': this.dataForm.shippingFee,
              'packFee': this.dataForm.packFee,
              'insureFee': this.dataForm.insureFee,
              'payFee': this.dataForm.payFee,
              'discount': this.dataForm.discount,
              'integral': this.dataForm.integral,
              'integralMoney': this.dataForm.integralMoney,
              'bonusId': this.dataForm.bonusId,
              'bonusMoney': this.dataForm.bonusMoney,
              'balanceMoney': this.dataForm.balanceMoney,
              'moneyPaid': this.dataForm.moneyPaid,
              'orderStatus': this.dataForm.orderStatus,
              'orderScore': this.dataForm.orderScore,
              'operator': this.dataForm.operator,
              'createUtc': this.dataForm.createUtc,
              'modifyUtc': this.dataForm.modifyUtc,
              'isDeleted': this.dataForm.isDeleted,
              'version': this.dataForm.version
            }
            var tick = !this.dataForm.id ? this.$http({
              url: this.$http.adornUrl('/generator/upktradeorder/save'),
              method: 'post',
              data: this.$http.adornData(params)
            }) : this.$http({
              url: this.$http.adornUrl('/generator/upktradeorder/update'),
              method: 'post',
              data: this.$http.adornData(params)
            })
            tick.then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
